package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,
                    BaseCategoryTrademark>implements BaseCategoryTrademarkService {

    @Autowired
    BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {

        QueryWrapper<BaseCategoryTrademark> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(wrapper);

        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //获取品牌id
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            return baseTrademarkMapper.selectBatchIds(idList);
        }
        return null;
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {

        QueryWrapper<BaseCategoryTrademark> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(wrapper);

        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null).
                                                    stream().filter(baseTrademark -> {
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());

            return baseTrademarkList;
        }
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {

        List<BaseCategoryTrademark> trademarkIdList = categoryTrademarkVo.getTrademarkIdList().stream().map(trademarkId->{
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(trademarkId);
            return baseCategoryTrademark;
        }).collect(Collectors.toList());
        this.saveBatch(trademarkIdList);
    }

    @Override
    public void removeCategoryandAll(Long category3Id, Long trademarkId) {

        QueryWrapper<BaseCategoryTrademark> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);
        wrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(wrapper);
    }
}
